package cn.upfinder.datalibrary.Api;

import android.content.Context;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

/**
 * 封装一个retrofit集成0kHttp3的抽象基类
 */
public class RetrofitManage {

    private static Retrofit retrofit;
    private static Retrofit souKuRetrofit;
    private static OkHttpClient okHttpClient;
    private static RetrofitManage retrofitManage;


    public static RetrofitManage getInstance() {

        if (retrofitManage == null) {
            synchronized (RetrofitManage.class) {
                if (retrofitManage == null) {
                    retrofitManage = new RetrofitManage();
                }
            }
        }
        return retrofitManage;
    }

    private RetrofitManage() {

    }

    /**
     * 获取GankRetrofit对象
     */
    public Retrofit getRetrofit(Context context) {

        if (null == retrofit) {

            if (null == okHttpClient) {
                okHttpClient = OkHttp3Manage.getInstance(context).getOkHttpClient();
            }
            //Retrofit2后使用build设计模式
            retrofit = new Retrofit.Builder()
                    //设置服务器路径
                    .baseUrl("http://gank.io")
                    //添加转化库，默认是Gson  //返回字符串
                    .addConverterFactory(ScalarsConverterFactory.create())
                    //添加回调库，采用RxJava
                    .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                    //设置使用okhttp网络请求
                    .client(okHttpClient)
                    .build();
        }
        return retrofit;
    }

    /**
     * 获取 优酷 Retrofit对象
     */
    public Retrofit getYouKuRetrofit(Context context) {

        if (null == retrofit) {

            if (null == okHttpClient) {
                okHttpClient = OkHttp3Manage.getInstance(context).getOkHttpClient();
            }
            //Retrofit2后使用build设计模式
            retrofit = new Retrofit.Builder()
                    //设置服务器路径
                    .baseUrl("http://list.youku.com")
                    //添加转化库，默认是Gson  //返回字符串
                    .addConverterFactory(ScalarsConverterFactory.create())
                    //添加回调库，采用RxJava
                    .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                    //设置使用okhttp网络请求
                    .client(okHttpClient)
                    .build();
        }
        return retrofit;
    }


    /*
    * 获取搜酷 reftofit
     */
    public Retrofit getSouKuRetrofit(Context context) {
        if (souKuRetrofit == null) {
            if (okHttpClient == null) {
                okHttpClient = OkHttp3Manage.getInstance(context).getOkHttpClient();
            }
            souKuRetrofit = new Retrofit.Builder()
                    .baseUrl("http://www.soku.com/")
                    .addConverterFactory(ScalarsConverterFactory.create())
                    .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                    .client(okHttpClient)
                    .build();
        }
        return souKuRetrofit;
    }


}
